package com.lxk.service.impl;

import com.lxk.dao.*;
import com.lxk.pojo.*;
import com.lxk.service.SystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author JohnSoybean
 * @create 2020/4/9 17:03
 * @description
 */
@Service
public class SystemServiceImpl implements SystemService {

    @Autowired
    private AsMyMapper mm;
    @Autowired
    private CashoutMapper cm;
    @Autowired
    private RefundMapper rm;
    @Autowired
    private AppointMapper am;
    @Autowired
    private MeasureMapper measureMapper;
    @Autowired
    private ContactMapper ctm;
    @Autowired
    private WxUserMapper wum;
    @Autowired
    private OrdersMapper om;
    @Autowired
    private PointOrdersMapper pom;


    public Integer getFahuoNum() {
        Integer count = mm.countOrders(1);
        return count;
    }

    public Integer getZhongjiangNum() {
        PointOrdersExample example = new PointOrdersExample();
        PointOrdersExample.Criteria criteria = example.createCriteria();
        criteria.andIsSuccessEqualTo(1);
        criteria.andExpressNoIsNull();
        Integer count = (int)pom.countByExample(example);
        return count;
    }

    public Integer getTixianNum() {
        CashoutExample example = new CashoutExample();
        CashoutExample.Criteria criteria = example.createCriteria();
        criteria.andIsFinishedEqualTo(0);
        Integer count = (int)cm.countByExample(example);
        return count;
    }

    public Integer getTuikuanNum() {
        RefundExample example = new RefundExample();
        RefundExample.Criteria criteria = example.createCriteria();
        criteria.andRefundStatusEqualTo(0);
        Integer count = (int)rm.countByExample(example);
        return count;
    }

    public Integer getYuyueNum() {
        AppointExample example = new AppointExample();
        AppointExample.Criteria criteria = example.createCriteria();
        criteria.andIsFinishedEqualTo(0);
        Integer count = (int)am.countByExample(example);
        return count;
    }

    public Integer getLiangfangNum() {
        MeasureExample example = new MeasureExample();
        MeasureExample.Criteria criteria = example.createCriteria();
        criteria.andIsFinishedEqualTo(0);
        Integer count = (int)measureMapper.countByExample(example);
        return count;
    }

    public Integer getJiamengNum() {
        ContactExample example = new ContactExample();
        ContactExample.Criteria criteria = example.createCriteria();
        criteria.andStatusEqualTo(0);
        Integer count = (int)ctm.countByExample(example);
        return count;
    }

    public List<Date> getLastSevenDays() {

        //将初始时间设为当天的0点0分0秒
        Calendar cal = Calendar.getInstance();
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        cal.add(Calendar.SECOND, -1);
        cal.add(Calendar.DAY_OF_MONTH, -6);

        List<Date> dateList = new ArrayList<>();
        for (int i = 0; i < 7; i++) {
            dateList.add(cal.getTime());
            cal.add(Calendar.DAY_OF_MONTH, 1);
        }

        return dateList;
    }

    public List<Integer> getLastSevenPopulation(List<Date> days) {
        List<Integer> popList = new ArrayList<>();
        WxUserExample example = new WxUserExample();
        for (Date day : days) {
            example.clear();
            WxUserExample.Criteria criteria = example.createCriteria();
            criteria.andDateAddedLessThan(day);
            Integer population = (int)wum.countByExample(example);
            popList.add(population);
        }
        return popList;
    }

    public List<BigDecimal> getLastSevenSales(List<Date> days) {
        Calendar cal = Calendar.getInstance();
        List<BigDecimal> saleList = new ArrayList<>();
        for (Date day : days) {
            //设置时间间隔
            cal.setTime(day);
            cal.add(Calendar.DAY_OF_MONTH, -1);
            Date yesterday = cal.getTime();
            BigDecimal sale = mm.selSalesSum(yesterday, day);
            if (sale == null) {
                sale = new BigDecimal(0);
            }
            saleList.add(sale);
        }
        return saleList;
    }

    public List<String> formatDate(List<Date> dates) {
        List<String> formats = new ArrayList<>();
        SimpleDateFormat df = new SimpleDateFormat("M月d日");
        for (Date date : dates) {
            String format = df.format(date);
            formats.add(format);
        }
        return formats;
    }
}
